package cn.dnf.user.service.impl;

import cn.dnf.basic.constant.RegisterConstant;
import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.service.impl.BaseServiceImpl;

import cn.dnf.basic.util.Md5Utils;
import cn.dnf.basic.util.StrUtils;
import cn.dnf.user.domain.Logininfo;
import cn.dnf.user.mapper.LogininfoMapper;
import cn.dnf.user.domain.User;
import cn.dnf.user.dto.RegisterDto;
import cn.dnf.user.mapper.UserMapper;
import cn.dnf.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.rmi.runtime.Log;
import sun.security.provider.MD5;

import javax.annotation.Resource;

@Service
//父类有事务,该注解有继承性
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();
        String verifyCode = dto.getVerifyCode();
        String password = dto.getPassword();
        String confirmPassword = dto.getConfirmPassword();
        //1.校验 非空校验
        if(StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(verifyCode)
                ||StringUtils.isEmpty(password)
                ||StringUtils.isEmpty(confirmPassword)
        ){
            throw new BusinessException("参数不能为空!");
        }
        //2.两次密码是否一致
        if(!password.equals(confirmPassword)){
            throw new BusinessException("两次密码不一致,请重新输入!");
        }
        //3.手机号校验,是否被注册
        User userPhone = userMapper.findByPhone(phone);
        if(userPhone!=null){
            throw new BusinessException("手机已经注册,请直接登录!");
        }
        //4.验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + RegisterConstant.PHONEREGISTER);
        if(obj==null){
            throw new BusinessException("验证码已过期,请重新获取");
        }
        //5.校验验证码是否正确
        String redisCode = obj.toString().split(":")[0];
        if(!verifyCode.equalsIgnoreCase(redisCode)){
            throw new BusinessException("验证码输入错误,请重新输入");
        }
        //6.保存数据 user, logininfo
        //先去获取logininfo对象和user对象
        Logininfo logininfo = dto2Logininfo(dto);
        //通过logininfo对象,获取user对象
        User user = logininfo2User(logininfo);
        //先保存logininfo表,返回自增的主键
        logininfoMapper.save(logininfo);
        //把自增的主键赋值给user对象,再保存user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private User logininfo2User(Logininfo logininfo) {
        User user = new User();
        //copy相同的属性
        BeanUtils.copyProperties(logininfo,user);
        return user;
    }

    public Logininfo dto2Logininfo(RegisterDto dto){
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);

        //生成盐值 32
        String salt = StrUtils.getComplexRandomString(32);

        //加盐加密
        String dbPassword = Md5Utils.encrypByMd5(salt + dto.getPassword());
        logininfo.setSalt(salt);
        logininfo.setPassword(dbPassword);

        return logininfo;
    }
}
